package com.pet.shop.service.order;

import com.github.pagehelper.Page;
import com.pet.exceptions.BusinessException;
import com.pet.shop.dto.PageDto;
import com.pet.shop.entity.order.Order;
import com.pet.shop.entity.room.Hotel;
import com.pet.shop.entity.room.Room;
import com.pet.shop.mappers.order.OrderMapper;
import com.pet.shop.mappers.room.HotelMapper;
import com.pet.shop.mappers.room.RoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements IOrderService{

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private HotelMapper hotelMapper;

    @Override
    public int deleteEntity(Long id) {
        return orderMapper.delete(id);
    }

    @Override
    public int deleteEntityList(List<Long> idList) {
        int i = 0;
        for(Long id : idList){
            i += orderMapper.delete(id);
        }
        return i;
    }

    @Override
    public Order insertEntity(Order record) {
        if(record == null){
            throw new BusinessException("预约信息不能为空");
        }
        if(record.getUserId() == null || record.getPetId() == null){
            throw new BusinessException("预约人与宠物信息不能为空");
        }
        if(record.getHotelId() == null){
            throw new BusinessException("预约酒店信息不能为空");
        }
        if(record.getOrderDate() == null || record.getTimeSlotId() == null ||  record.getTimeSlot() == null){
            throw new BusinessException("预约时间不能为空");
        }
        Hotel hotel = hotelMapper.get(record.getHotelId());
        if(hotel == null){
            throw new BusinessException("预约酒店信息不存在");
        }
        record.setPrice(hotel.getPrice());
        record.setStatus(1);
        //查找时间段可以预约的房间id
        List<Long> roomIdList = roomMapper.queryIdByHotelId(record.getHotelId());
        if(CollectionUtils.isEmpty(roomIdList)){
            throw new BusinessException("客房无可用房间不能预约");
        }
        List<Long> orderRoomIdList = orderMapper.queryRoomId(record.getHotelId(), record.getOrderDate(), record.getTimeSlotId());
        roomIdList.removeAll(orderRoomIdList);
        if(CollectionUtils.isEmpty(roomIdList)){
            throw new BusinessException("客房已满不能预约");
        }
        record.setRoomId(roomIdList.get(0));
        record.setSn(UUID.randomUUID().toString().replaceAll("-", ""));
        orderMapper.insert(record);
        return record;
    }

    @Override
    public Order getEntity(Long id) {
        return orderMapper.get(id);
    }

    @Override
    public int updateEntity(Order record) {
        if(record == null){
            throw new BusinessException("订单信息不能为空");
        }
        return orderMapper.update(record);
    }

    @Override
    public PageDto<Order> queryPage(PageDto<Order> pageDto) {
        Page<Order> page =  orderMapper.queryPage(pageDto);
        pageDto.setPageResult(page);
        return pageDto;
    }

    @Override
    public List<Order> queryOrderByParamMap(Map<String, Object> paramMap) {
        List<Order> orderList = orderMapper.queryByParamMap(paramMap);
        if(orderList == null) return new ArrayList<>();
        List<Long> roomIdList = orderList.stream().map(Order::getRoomId).collect(Collectors.toList());
        Map<Long, List<Order>> roomIdOrderMap = orderList.stream().collect(Collectors.groupingBy(Order::getRoomId));
        List<Room> roomList = roomMapper.queryByIdList(roomIdList);
        roomList.forEach(room -> {
            List<Order> orders = roomIdOrderMap.get(room.getId());
            if(orders != null) orders.forEach(order -> {
                order.setMonitorSn(room.getMonitorSn());
                order.setMonitorId(room.getMonitorId());
                order.setVideoAddress(room.getVideoAddress());
            });
        });
        return orderList;
    }

}
